package controllers.tools;

import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.lang.Character;

import models.Collaborator;
import models.Formation;
import models.Mission;
import models.Nationality;
import models.Profile;
import models.Skill;
import models.SkillCategory;

import org.hibernate.collection.PersistentSet;

/**
 * This class provides tools methods usable in view or in controllers
 * 
 * @author Elian ORIOU
 * 
 */

public class Tools {

	public static final DateFormat _DATE_FORMATTER = new SimpleDateFormat(
			"dd/MM/yyyy");

	/**
	 * Returns the trigram of a collaborator (first letter of his name + first
	 * letter of his firstname + last letter of his first name)
	 * 
	 * @param collaborator
	 * @return
	 */

	public static String generateCollaboratorTrigram(Collaborator collaborator) {

		if (collaborator == null || collaborator.getValidProfile() == null) {
			return null;
		}
		String name = collaborator.getValidProfile().getName();
		String firstName = collaborator.getValidProfile().getFirstname();

		if (name.isEmpty() || firstName.isEmpty()) {
			return "";
		}

		return Character.toString(name.charAt(0)).toUpperCase()
				+ Character.toString(firstName.charAt(0)).toUpperCase()
				+ Character.toString(firstName.charAt(firstName.length() - 1))
						.toUpperCase();
	}

	/**
	 * Transforms a date into string following the french date format
	 * 
	 * @param d
	 * @return
	 */

	public static String getDateAsString(Date d) {
		if (d == null) {
			return "";
		}
		return _DATE_FORMATTER.format(d);
	}

	/**
	 * get all the nationalities as a simple string
	 * 
	 * @param nationality
	 * @return
	 */

	public static String getNationalitiesAsString(PersistentSet nationalities) {
		StringBuilder builder = new StringBuilder();
		Iterator it = nationalities.iterator();
		for (; it.hasNext();) {
			Nationality nat = (Nationality) it.next();
			builder.append(nat.getName() + " ");
		}
		return builder.toString();
	}

	/**
	 * 
	 * @param collaborator
	 * @return all his skills
	 */

	public Map<SkillCategory, List<Skill>> getAllSkills(Collaborator collab) {
		Profile profile = collab.getValidProfile();
		Map<SkillCategory, List<Skill>> result = new HashMap<SkillCategory, List<Skill>>();

		Set<Mission> allMissions = profile.getMissions();
		for (Mission mission : allMissions) {
			Set<Skill> allMissionSkills = mission.getMissionSkills();
			for (Skill skill : allMissionSkills) {
				if (result.get(skill.getCategory()) == null) {
					List<Skill> skills = new ArrayList<Skill>();
					skills.add(skill);
					result.put(skill.getCategory(), skills);
				} else {
					List<Skill> skills = result.get(skill.getCategory());
					if (skills.contains(skill) == false) {
						skills.add(skill);
					}
				}
			}
		}

		Set<Formation> allFormations = profile.getFormations();
		for (Formation formation : allFormations) {
			Set<Skill> allFormationSkills = formation.getFormationSkills();
			for (Skill skill : allFormationSkills) {
				if (result.get(skill.getCategory()) == null) {
					List<Skill> newSkills = new ArrayList<Skill>();
					newSkills.add(skill);
					result.put(skill.getCategory(), newSkills);
				} else {
					List<Skill> skills = result.get(skill.getCategory());
					if (skills.contains(skill) == false) {
						skills.add(skill);
					}
				}
			}
		}

		for (List<Skill> skills : result.values()) {
			Collections.sort(skills);
		}

		return result;
	}
}